//
//  ExtensionInterfacVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/12.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "ExtensionInterfacVC.h"

@interface ExtensionInterfacVC ()
{
    UIImageView *imageView_QRCode;//付款 界面
}

@property (strong, nonatomic) UIWebView *browseWeb;

@end

@implementation ExtensionInterfacVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推广";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    [self initWithExtensionInterfaceVCSubviews];
    
}

- (void) initWithExtensionInterfaceVCSubviews
{
    
    if (![self.str_ycaptcha isEqual:[NSNull null]])
    {
        //big imageview
        imageView_QRCode = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-160)/2, 120, 160, 160)];
        
        imageView_QRCode.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:imageView_QRCode];
        [self showMyCode];
    }
    
    UILabel * label_amuont;
//    if ([self.str_ycaptchanum intValue] !=0 )
//    {
        label_amuont = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView_QRCode.bottom+20, kScreenWidth, 20)];
        
        label_amuont.textColor = [UIColor whiteColor];
        label_amuont.textAlignment = NSTextAlignmentCenter;
        label_amuont.font = [UIFont systemFontOfSize:Font_SIZE_30];
        label_amuont.textAlignment = NSTextAlignmentCenter;
        
        label_amuont.textColor = COLOT_TEXTGRAY;

        [self.view addSubview:label_amuont];
        label_amuont.text = [NSString stringWithFormat:@"已参与人数 : %@  人",self.str_ycaptchanum];
//    }
    
    //webview
    self.browseWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, label_amuont.bottom, kScreenWidth, kScreenHeight-label_amuont.y-label_amuont.height)];
    [self.view addSubview:self.browseWeb];
    
    NSURL *url=[NSURL URLWithString:@"http://www.mrbang.com.cn/spread.php"];
    [self.browseWeb loadRequest:[NSURLRequest requestWithURL:url]];
    //此处是处理网页黑条条的状况
    self.browseWeb.opaque = NO;
    self.browseWeb.backgroundColor = [UIColor clearColor];
    [self.browseWeb setScalesPageToFit:NO];
}

#pragma mark --展示二维码
- (void)showMyCode {
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    
    //这里改成自己想要生成二维码 所对应的字符串
    NSString *dataStr = self.str_ycaptcha;
    
    NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *outputImage = [filter outputImage];
    
    imageView_QRCode.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:imageView_QRCode.width];
    
}

#pragma mark --- 生成二维码
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
